/*
 * Lesson.java
 *
 * Created on 28. Januar 2007, 21:37
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.lesson;

import java.util.*;

/**
 * Lesson mantains a list of sections, which contain the entries that should be learned.
 * Note that the sections and the bot class referenced are part of the persisted
 * state of the lesson (e.g. in the XML based lsn format), while other settings 
 * (active sections, max number of entries) are not persisted here, but in the 
 * user preferences.
 * @author Pirx
 */
public class Lesson implements Iterable<Section> {
    
    private List<Section> sections = new ArrayList<Section>();

    private String name;
    private String description;
    private String author;
    private String botType;
    private Map<String, String> botParams = new HashMap<String, String>();
    private int maxEntries = Integer.MAX_VALUE;
    
    /**
     * Creates a new instance of Lesson
     * @param name The name of the Lesson
     * @param description A description of the lesson.
     */
    public Lesson(String name, String description) {
        this.setName(name);
        this.setDescription(description);
    }
    
    /**
     * Creates a new instance of Lesson
     */
    public Lesson() {
    }
    
    /**
     * Getter for the lesson name
     * @return The name of the lesson
     */
    public String getName() {
        return name;
    }
    
    /**
     * Getter for the lesson description
     * @return The description of the lesson
     */
    public String getDescription() {
        return description;
    }
    
    /**
     * Appends a new section to this lesson
     * @param section the section to be added
     */
    public void addSection(Section section) {
        sections.add(section);
    }

    /**
     * Implements the Iterable<Section> interface
     * @return The Iterator over all sections
     */
    public Iterator<Section> iterator() {
        return sections.iterator();
    }
    
    /**
     * Number of Sections
     * @return The number of sections in this lesson
     */
    public int size() {
        return sections.size();
    }
    
    /**
     * Getter for a section
     * @param index The index of the section
     * @return The section at the specified index
     */
    public Section getSection(int index) {
        return sections.get(index);
    }

    /**
     * Returns the entries of this lesson according to the current settings.
     * Only Entries from active sections are included. The list of entries
     * is shuffled and contains not more that getMaxEntries() entries.
     * @return A shuffled list of entries from the active sections, not more than getMaxEntries()
     */
    public List<Entry> getEntries() {
        //Level calls entries.remove(0), so LinkedList is better
        List<Entry> entries = new LinkedList<Entry>(); 
        for (Section section : sections) {
            if (section.isIncluded()) {
                entries.addAll(section.getEntries());
            }
        }
        Collections.shuffle(entries);
        return (entries.size() > maxEntries) 
                ? entries.subList(0,maxEntries) 
                : entries;
    }
    
    /**
     * The list of entries from all active sections, not shuffled.
     * @return The list of all entries from the active sections
     */
    public List<Entry> getAllSelectedEntries() {
        List<Entry> entries = new ArrayList<Entry>();
        for (Section section : sections) {
            if (section.isIncluded()) {
                entries.addAll(section.getEntries());
            }
        }
        return entries;
    }

    /**
     * The list of all entries, not shuffled, from active and inactive sections.
     * @return The list of all entries
     */
    public List<Entry> getAllEntries() {
        List<Entry> entries = new ArrayList<Entry>();
        for (Section section : sections) {
            entries.addAll(section.getEntries());
        }
        return entries;
    }

    /**
     * Getter for the qualified class name of the bot class that should be used with this lesson.
     * Currently the only available bot classes are kanjitori.graphics.bot.KanjiBot and
     * kanjitori.graphics.bot.FlagBot
     * @return The qualified name of the bot class
     */
    public String getBotType() {
        return botType;
    }

    /**
     * Setter for the qualified class name of the bot class that should be used with this lesson.
     * Currently the only available bot classes are kanjitori.graphics.bot.KanjiBot and
     * kanjitori.graphics.bot.FlagBot
     * @param botClass The qualified name of the bot class
     */
    public void setBotType(String botType) {
        this.botType = botType;
    }

    /**
     * Setter for the lesson name
     * @param name The name of the lesson
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Setter for the lesson description
     * @param description The description of this lesson
     */
    public void setDescription(String description) {
        this.description = description;
    }
    
    /**
     * Setter for the maxEntries property.
     * @param maxEntries The maximum number of entries
     * @see #getEntries
     */
    public void setMaxEntries(int maxEntries) {
        this.maxEntries = maxEntries;
    }
    
    /**
     * Getter for the maxEntries properties
     * @return The maximum number of entries
     * @see #getEntries
     */
    public int getMaxEntries() {
        return maxEntries;
    }

    /**
     * Getter for the bot parameters
     * @return The paramters of the bot
     */
    public Map<String, String> getBotParams() {
        return botParams;
    }

    /**
     * Setter for the bot parameters
     * @param botParams The paramters of the bot
     */
    public void setBotParams(Map<String, String> botParams) {
        this.botParams = botParams;
    }

    /**
     * Getter for the author
     * @return The author of this lesson
     */
    public String getAuthor() {
        return author;
    }

    /**
     * Setter for the author
     * @param author The author of this lesson
     */
    public void setAuthor(String author) {
        this.author = author;
    }
}
